package cc.fs.zhihuapp.ui.activity.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by fostion on 12/17/15.
 */
public class RxJavaDemo extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * observer使用方式
         */
        getObserver();

    }

    private void e(String msg) {
        Log.e(" --RxAndroid-- ", msg);
    }


    /**
     * observer 和 subscriber是一样的使用方式，不同在于:
     * subscriber多了onStart()
     */
    public void getObserver() {
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onCompleted() {
//                but.setVisibility(View.VISIBLE);
                e(" observable  onCompleted   ");
            }

            @Override
            public void onError(Throwable e) {
                e("  observable  onError   ");
            }

            @Override
            public void onNext(String s) {
                e(" observable  onNext   ");
            }
        };


        Subscriber<String> subscriber = new Subscriber<String>() {

            @Override
            public void onStart() {
                super.onStart();
                e(" subscriber  onStart   ");
            }

            @Override
            public void onCompleted() {
                e(" subscriber  onCompleted   ");
            }

            @Override
            public void onError(Throwable e) {
                e(" subscriber  onError   ");
            }

            @Override
            public void onNext(String s) {
                e(" subscriber  onNext   ");
            }
        };

        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("Hello");
                subscriber.onNext("Hi");
                subscriber.onNext("Aloha");
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());


        observable.subscribe(observer);
        e("--------------------------------------");
        observable.subscribe(subscriber);


        e("-----------------转换数据类型---------------------");
        Observable.just("test")
                .subscribeOn(Schedulers.newThread())
                .map(new Func1<String, Integer>() {
                    @Override
                    public Integer call(String s) {
                        e("result -- " + s);
                        return 123456;
                    }
                })
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        e("result -- " + integer);
                        return "ok true";
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(String s) {
                        e("result  ssss -- " + s);
                    }
                });


        e("-----------------过滤---------------------");
        List<Integer> ints = new ArrayList<>();
        ints.add(1);
        ints.add(2);
        ints.add(3);
        ints.add(3);
        ints.add(3);
        ints.add(4);
        ints.add(4);
        ints.add(4);
        ints.add(4);
        ints.add(5);
        ints.add(6);
        Observable.from(ints)
                .map(new Func1<Integer, Integer>() {
                    @Override
                    public Integer call(Integer integer) {
                        return integer - 2;
                    }
                })
                .filter(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer >= 3;
                    }
                })
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        e("---过滤结果-  " + integer);
                    }
                });


    }

    @Override
    public void handler(Message msg) {

    }
}
